// vue3.0,vue-router需要4.0,element-ui需要用element-plus

import { createApp } from 'vue'
import App from './App.vue'
import routes from './router'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import './assets/css/element-variables.scss'
import './assets/css/reset.scss'
import './assets/css/common.scss'
import store from './store/index.js'
import Init from './init/index.js'
import { createRouter, createWebHistory } from 'vue-router'

let routerHistory
let router
let all_menu = []



const render = () => {
    routerHistory = createWebHistory()
    router = createRouter({
        history: routerHistory,
        routes: all_menu
    })
    router.beforeEach((to, from, next) => {
        // let identity = ["dev", "testing", "operation", "product" ]
        // if(identity.indexOf(store.state.userAuth) < 0 && to.name != 'calculator') {
        //   return false
        // }else {
        //   return true
        // }
        next()
        // return true

    })
}
const FnGetRouter = () => {
    all_menu.push(routes[0])
    for (let route of routes) {
        if (route.children) {
            route.children.forEach(item => {
                if (item.children && store.state.authRouterInfo[item.name]) {
                    let child_list = []
                    item.children.forEach(child => {
                        if (child.chidren && store.state.authRouterInfo[child.meta.name] && store.state.authRouterInfo[child.meta.name].indexOf(child.name) !== -1) {
                            let grandSonList = []
                            child.children.forEach(grandSon => {
                                grandSonList.push(grandSon)
                            })
                            if (grandSonList.length > 0) {
                                child_list.push({
                                    ...child,
                                    children: grandSonList
                                })
                            }
                        } else if (store.state.authRouterInfo[child.meta.name]) {
                            child_list.push(child)
                        }
                    })
                    all_menu.push({
                        ...item,
                        children: child_list
                    })
                } else {
                    if (store.state.authRouterInfo[item.name]) {
                        all_menu.push(item)
                    } else if (item.name === 'console') {
                        all_menu.push(item)
                    }
                }
            })
        }
    }

}

let FnInit = async () => {
    await Init.get_userinfo() // 获取用户信息
    FnGetRouter()
    render()
    const app = createApp(App)
    app.config.globalProperties.$store = store
    app.use(router).use(ElementPlus, { locale: zhCn }).mount('#app')
}
FnInit()
